//
//  ViewController.m
//  tableView重用机制索引条
//
//  Created by 崔博 on 2018/6/20.
//  Copyright © 2018年 崔博. All rights reserved.
//

#import "ViewController.h"
#import "IndexTableView.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,IndexTableViewDataSource>
{
    IndexTableView *tableView;
    UIButton *button;
    NSMutableArray *dataSource;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    tableView = [[IndexTableView alloc] initWithFrame:CGRectMake(0, 100,self.view.frame.size.width , self.view.frame.size.height - 100)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.indexedDataSource = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:tableView];
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, self.view.frame.size.width, 100);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(doAnticon:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"reload" forState:0];
    [self.view addSubview:button];
    
    dataSource = [NSMutableArray array];
    for (int i = 0; i < 100; i++) {
        [dataSource addObject:@(i + 1)];
    }
    
}
- (void)doAnticon:(UIButton *)sender
{
    [tableView reloadData];
}

#pragma mark - delegate datasource
- (NSArray<NSString *> *)indexTitlesForIndxTable:(UITableView *)tableView
{
    static BOOL change = NO;
    if (change) {
        change = NO;
        return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K"];
    }else
    {
        change = YES;
        return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G"];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",dataSource[indexPath.row]];
    return cell;
}
@end
